
# Definition for a Node.
class Node:
    def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
        self.val = val
        self.left = left
        self.right = right
        self.next = next

root=Node(1)
root.left=Node(2)
root.right=Node(3)
root.left.left=Node(4)
root.left.right=Node(5)
root.right.left=Node(6)
root.right.right=Node(7)

def connect(root):
    #空树的情况
    if not root:
        return None
    queue=[root]
    while len(queue)>0:
        queueLen=len(queue)
        pre=queue[0]
        while queueLen>0:
            node=queue.pop(0)
            if pre==node:
                #第一个结点
                pass
            else:
                pre.next=node
                pre=pre.next

            if node.left:
                queue.append(node.left)
            if node.right:
                queue.append(node.right)
            queueLen-=1

    while root:
        print(root.val)
        root=root.next

connect(root)